using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TowerDefence.Tiles;
using GraphicsLib;
using TowerDefence.UI;

namespace TowerDefence
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        Graphics2D graphics2D;
        SpriteBatch spriteBatch;        
        Map map;
        Rectangle windowArea;
        World world;
        UIManager uiManager;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
            this.graphics.PreferredBackBufferWidth = this.map.TileWidth * this.map.Width;
            this.graphics.PreferredBackBufferHeight = this.map.TileHeight * this.map.Height;
            this.graphics.ApplyChanges();
           

            var tileFactory = new TileFactory();
            var tiles = tileFactory.CreateTiles(this.map);
            this.world = new World(tiles, map);
        }

        protected override void LoadContent()
        {
            this.spriteBatch = new SpriteBatch(GraphicsDevice);          
            Texture2D basicTexture = Content.Load<Texture2D>("misc/whitepx");
            this.graphics2D = new Graphics2D(this.spriteBatch, basicTexture);
            
            this.map = Map.Load("Content/maps/example_fourAiPaths.tmx", this.Content);

            this.windowArea = new Rectangle(0, 0, this.Window.ClientBounds.Height, this.Window.ClientBounds.Height);
            SpriteFont font = Content.Load<SpriteFont>("fonts/MenuFont");
            this.uiManager = new UIManager(this.spriteBatch, this.graphics2D, font, windowArea);
            this.uiManager.Load(this.Content);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            this.uiManager.Update();
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Green);

            this.spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            this.map.Draw(this.spriteBatch, this.windowArea, new Vector2(0, 0));
            this.world.DebugDraw(this.graphics2D);
            this.uiManager.Draw();
            this.spriteBatch.End();		

            base.Draw(gameTime);
        }
    }
}
